package itsm.isperp.module.service.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.module.entity.app.AppPortalRole;
import itsm.isperp.module.entity.app.AppUserWebosMenu;
import itsm.isperp.module.repository.app.AppPortalRoleMapper;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @date 2014-2-24
 * @author Lizx
 */
@Service
public class AppPortalRoleService extends BaseService<AppPortalRole> {

	@Resource
	protected AppPortalRoleMapper appPortalRoleMapper;

	@Override
	public AppPortalRoleMapper getRepository() {
		return this.appPortalRoleMapper;
	}

	public List<String> findRolesByPortalId(String appPortalId) {
		return appPortalRoleMapper.findRolesByPortalId(appPortalId);
	}

	public void deleteByPortalId(String appPortalId) {
		appPortalRoleMapper.deleteByPortalId(appPortalId);
	}

	public void saveBatch(String appPortalId, String[] roles) {
		
		String sql = "INSERT INTO app_portal_role(id,app_portal_id,app_role_id)VALUES(?,?,?)";

		JdbcTemplate jdbcTemplate = ContextHolder
				.getSpringBean("jdbcTemplate");
		List<Object[]> list = new ArrayList<>();

		if (roles != null && roles.length > 0) {
			for (String m : roles) {
				Object[] arg = new Object[3];
				arg[0] = HibernateUUIDGenerator.generate();
				arg[1] = appPortalId;
				arg[2] = m;
				
				
				list.add(arg);
			}

			jdbcTemplate.batchUpdate(sql, list);
		}

	}

}
